﻿using System;
using System.Globalization;

namespace UtilZ.Dotnet.Ex.Base
{

#if CORE

    /// <summary>
    /// TimeOnly类型转换接口
    /// </summary>
    public class TimeOnlyTypeConverter : TypeConverterAbs
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        public TimeOnlyTypeConverter()
            : base(typeof(TimeOnly))
        {

        }


        /// <summary>
        /// 尝试将字符串转换为值类型数据,包括枚举
        /// </summary>
        /// <param name="targetType">要待转换的目标类型</param>
        /// <param name="value">要转换的值</param>
        /// <param name="result">转换结果</param>
        /// <param name="style">字符串中数值样式[数字样式为NumberStyles值之一，日期时间为DateTimeStyles值之一,默认null为不使用样式]</param>
        /// <param name="formatProvider">一个提供区域性特定的格式设置信息的对象</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(Type targetType, string value, out object result, object style = null, IFormatProvider formatProvider = null)
        {
            TimeOnly targetResult;
            bool parseResult;
            if (style != null)
            {
                parseResult = TimeOnly.TryParse(value, formatProvider, (DateTimeStyles)style, out targetResult);
            }
            else
            {
                if (formatProvider == null)
                {
                    parseResult = TimeOnly.TryParse(value, out targetResult);
                }
                else
                {
                    parseResult = TimeOnly.TryParse(value, formatProvider, DateTimeStyles.None, out targetResult);
                }
            }

            if (parseResult)
            {
                result = targetResult;
            }
            else
            {
                result = null;
            }

            return parseResult;
        }




        /// <summary>
        /// 尝试将字符串转换为值类型数据,包括枚举
        /// </summary>
        /// <param name="targetType">要待转换的目标类型</param>
        /// <param name="value">要转换的值</param>
        /// <param name="result">转换结果</param>
        /// <param name="style">字符串中数值样式[数字样式为NumberStyles值之一，日期时间为DateTimeStyles值之一,默认null为不使用样式]</param>
        /// <param name="formatProvider">一个提供区域性特定的格式设置信息的对象</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(Type targetType, ReadOnlySpan<char> value, out object result, object style = null, IFormatProvider formatProvider = null)
        {
            TimeOnly dt;
            bool parseResult;
            if (style != null)
            {
                parseResult = TimeOnly.TryParse(value, formatProvider, (DateTimeStyles)style, out dt);
            }
            else
            {
                if (formatProvider == null)
                {
                    parseResult = TimeOnly.TryParse(value, out dt);
                }
                else
                {
                    parseResult = TimeOnly.TryParse(value, formatProvider, DateTimeStyles.None, out dt);
                }
            }

            if (parseResult)
            {
                result = dt;
            }
            else
            {
                result = null;
            }

            return parseResult;
        }


    }

#endif
}
